import json
import random
import time
from urllib import request

import numpy as np
from bs4 import BeautifulSoup

import mysql.connector
from fdfs_client.client import get_tracker_conf, Fdfs_client


def initMysql():
  global mydb
  global mycursor

  # mydb = mysql.connector.connect(
  #   host='8.129.219.66',
  #   user='guifangtong',
  #   passwd='guifangtong1234!@#',
  #   database='guifangtong'
  # )

  mydb = mysql.connector.connect(
    host='localhost',
    user='root',
    passwd='root1234',
    database='guifangtong'
  )

  mycursor = mydb.cursor()


def findFangs():
  initMysql()
  table_name = 't_fang'
  sql = "select * from " + table_name
  mycursor.execute(sql)
  myresult = mycursor.fetchall()  # fetchall() 获取所有记录
  return myresult


# 物业类型
estateTypeOldArr = [{'name': '住宅', 'value': 1}, {'name': '公寓', 'value': 2}, {'name': '商铺', 'value': 3},
                    {'name': '写字楼', 'value': 4},
                    {'name': '商住楼', 'value': 5}, {'name': '别墅', 'value': 6}, {'name': '叠拼', 'value': 7},
                    {'name': '洋房', 'value': 8},
                    {'name': '其他', 'value': 9}]
# 装修标准
fixTypeOldArr = [{'name': '毛坯', 'value': 1}, {'name': '精装', 'value': 2}]

# 建筑类型
buildTypeOldArr = [{'name': '低层', 'value': 1}, {'name': '小层', 'value': 2}, {'name': '高层', 'value': 3}]

# 楼盘特色
specialOldArr = [{'name': '小户型', 'value': 1}, {'name': '公园地产', 'value': 2}, {'name': '学区房', 'value': 3},
                 {'name': '旅游地产', 'value': 4},
                 {'name': '投资地产', 'value': 5}, {'name': '经济住宅', 'value': 6}, {'name': '宜居生态地产', 'value': 7},
                 {'name': '地铁口', 'value': 8},
                 {'name': '商场旁', 'value': 9}, {'name': '老城区', 'value': 10}, {'name': '中心地段', 'value': 11}]


def updateFangStatus(item):
  initMysql()
  table_name = 't_fang'
  #   print('item', item)
  # 物业类型
  estateType_str = item[51]
  estate_type_int_str = ''
  if estateType_str != None:
    estateType_str = estateType_str[1:len(estateType_str) - 1]
    estateTypeArr = estateType_str.split(',')
    estate_type_int = []
    for eobj in estateTypeArr:
      for eoobj in estateTypeOldArr:
        if eoobj['name'] in eobj:
          estate_type_int.append(eoobj['value'])
    estate_type_int_str = ','.join(str(i) for i in estate_type_int)
    print('物业类型', 'estate_type_int_str', estate_type_int_str)

  # 装修标准
  fixType_str = item[43]
  fix_type_int_str = ''
  if fixType_str != None:
    fixType_str = fixType_str[1:len(fixType_str) - 1]
    fixTypeArr = fixType_str.split(',')
    fix_type_int = []
    for fobj in fixTypeArr:
      for foobj in fixTypeOldArr:
        if foobj['name'] in fobj:
          fix_type_int.append(foobj['value'])
    fix_type_int_str = ','.join(str(i) for i in fix_type_int)
    print('装修标准', 'fix_type_int', fix_type_int_str)

  # 建筑类型
  buildType_str = item[42]
  build_type_int_str = ''
  if buildType_str != None:
    buildType_str = buildType_str[1:len(buildType_str) - 1]
    buildTypeArr = buildType_str.split(',')
    build_type_int = []
    for bobj in buildTypeArr:
      if '超' in bobj:
          print('bobj',bobj)
      else:
        for boobj in buildTypeOldArr:
          if boobj['name'] in bobj:
            build_type_int.append(boobj['value'])
    build_type_int_str = ','.join(str(i) for i in build_type_int)
    print('建筑类型', 'build_type_int_str', build_type_int_str)

  # 楼盘特色
  special_str = item[44]
  special_int_str = ''
  if special_str != None:
    special_str = special_str[1:len(special_str) - 1]
    specialArr = special_str.split(',')
    special_int = []
    for sobj in specialArr:
      for soobj in specialOldArr:
        if soobj['name'] in sobj:
          special_int.append(soobj['value'])
    special_int_str = ','.join(str(i) for i in special_int)
    print('楼盘特色', 'special_int_str', special_int_str)
  fangid = item[0]
  print('-------------------华丽分割线---------------------------',item[0])


  sqlArr = []
  if estate_type_int_str != '':
     sqlArr.append('estate_type_str = "'+ str(estate_type_int_str)+'"')
  if fix_type_int_str != '':
     sqlArr.append('fix_type_str = "'+ str(fix_type_int_str)+'"')
  if build_type_int_str != '':
     sqlArr.append('build_type_str = "'+ str(build_type_int_str)+'"')
  if special_int_str != '':
     sqlArr.append('special_str = "'+ str(special_int_str)+'"')

  cmd_sql = 'UPDATE ' + table_name + ' set '
  sql_str = ','.join(str(i) for i in sqlArr)
  cmd_sql = cmd_sql+sql_str + ' where id = '+ str(fangid)
  # cmd_sql = 'UPDATE ' + table_name + ' set estate_type = "' + str(estate_type_int_str) + '", fix_type= "' +str(fix_type_int_str)+ '", build_type= "' +str(build_type_int_str) + '", special= "' +str(special_int_str)+ '" where id = '+ str(fangid)
  print("更新sql", cmd_sql)
  try:
    mycursor.execute(cmd_sql)
    mydb.commit()
    print('更新成功了')
  except Exception as e:
    mydb.rollback()  # 发生错误时回滚
    print('更新出错了', e)
  mydb.close()



if __name__ == '__main__':

  resFangs = findFangs()
  for item in resFangs:
    updateFangStatus(item)
    # time.sleep(2)
